package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
<service android:name="com.yakir.services.MusicService">

        </service>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //开始
        public void start(String path) {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepare();
                mediaPlayer.start();
                MediaUtils.currentState= Constants.PLAY_START;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //暂停
        public void pause (){
            if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                MediaUtils.currentState=Constants.PLAY_PAUSE;
            }
        }
        //继续播放
        public void continuePlay() {
            if (mediaPlayer!=null&&!mediaPlayer.isPlaying()) {
                mediaPlayer.start();
                MediaUtils.currentState= Constants.PLAY_START;
            }
        }
        //停止播放
        public void stop() {
            if (mediaPlayer!=null) {
                mediaPlayer.stop();
                MediaUtils.currentState=Constants.PLAY_STOP;
            }
        }
        private void startMusicService(String option,String path) {
            Intent intentService = new Intent(MainActivity.this, MusicService.class);
            intentService.putExtra("option", option);
            intentService.putExtra("messenger",new Messenger(handler));
            intentService.putExtra("path", path);
            startService(intentService);
        }
        private void startMusicService(String option) {
            Intent intentService = new Intent(MainActivity.this, MusicService.class);
            intentService.putExtra("option", option);
            intentService.putExtra("messenger",new Messenger(handler));
            startService(intentService);
        }
        private void startMusicService(String option,int progress) {
            Intent intentService = new Intent(MainActivity.this, MusicService.class);
            intentService.putExtra("option", option);
            intentService.putExtra("progress",progress);
            intentService.putExtra("messenger",new Messenger(handler));
            startService(intentService);
        }
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e("thread",Thread.currentThread().getName() );
            String option=intent.getStringExtra("option");
            if (messenger==null) {
                messenger = (Messenger) intent.getExtras().get("messenger");
            }
            if ("开始".equals(option)) {
                start(intent.getStringExtra("path"));
            } else if ("暂停".equals(option)) {
                pause();
            } else if ("继续".equals(option)) {
                continuePlay();
            } else if ("停止".equals(option)) {
                stop();
            } else if ("跳转".equals(option)) {
                seekPlay(intent.getIntExtra("progress",-1));
            }
            return super.onStartCommand(intent, flags, startId);
        }
    }
}
